<?php
namespace Content\Controller;

use Base\Controller\AdminBaseController;

class PostController extends AdminBaseController {
    
	protected $postModel;
	
	function _initialize() {
		parent::_initialize();
		
		$this->postModel = D("Content/Post");
	}
	
	/******************************** JSON *********************************/
	/** 添加信息 */
	public function save() {
		$postJson = file_get_contents('php://input');
		$post = json_decode($postJson, true);
		
		// setting
		$post['status'] = 0;
		$post['add_time'] = date("Y-m-d H:i:s");
		$post['update_time'] = date("Y-m-d H:i:s");

		// 执行添加
		$result = $this->postModel->add($post);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '添加失败！';
			$this->ajaxReturn($data);
		}

		$newPost = $this->postModel->where(array("id" => $result))->find();
		
		// 如果前台未设置url，则设置
		if (empty($newPost['url'])) {
		    $newPost['url'] = C('CONTENT_RULE') . $post['cid'] . '&id=' . $id;
		    $this->postModel->where(array("id" => $id))->save($newPost);
		}
			
		$data['code'] = 0;
		$data['msg'] = '添加成功！';
		$data['obj'] = $newPost;
		$this->ajaxReturn($data);
	}

	/** 查看所有列表 */
	public function dataList() {
		$_GET['notEqualMap'] = array('status' => -1);
		$dataList = $this->postModel->dataList($_GET);
	    $this->ajaxReturn($dataList);
	}

	/** 根据cid查看所有列表 */
	public function dataListByPostCatId() {
	    $cid = $_GET['cid'];
	     
	    $catList = M("Category")->select();					// 添加筛选???
	    $idsList = $this->getIds($catList, $cid, []);
// 	    dump($idsList);
	    
		$_GET['inKey'] = 'cid';
		$_GET['inValue'] = $idsList;
		$_GET['notEqualMap'] = array('status' => -1);
		$dataList = $this->postModel->dataList($_GET);
		
	    $this->ajaxReturn($dataList);
	}
	 
	/** 查看某信息 */
	public function find() {
		$id = $_GET['id'];
		
		$post = $this->postModel->where(array("id" => $id))->find();
		if (!$post) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
	    $data['code'] = 0;
	    $data['msg'] = 'OK';
	    $data['obj'] = $post;
	    $this->ajaxReturn($data);
	}

	/** 修改某信息 */
	public function update() {
		$id = $_GET['id'];
		$postJson = file_get_contents('php://input');
		$post = json_decode($postJson, true);

		// update
		$post['url'] = '';											// update reset
		$post['status'] = 0;										// update reset
		$post['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->postModel->where(array("id" => $id))->save($post);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '编辑失败！';
			$this->ajaxReturn($data);
		}

		$updatedPost = $this->postModel->where(array("id" => $id))->find();
		$data['code'] = 0;
		$data['msg'] = '编辑成功！';
		$data['obj'] = $updatedPost;
		$this->ajaxReturn($data);
	}

	/** 批量刷新（URL） */
	public function batRefreash() {
		$result = $this->postModel->select();
		foreach ($result as $v) {
// 		    $v['status'] = 0;
			$v['url'] = C('CONTENT_RULE') . $v['id'];
			$this->postModel->where(array("id" => $v['id']))->save($v);
		}
		
		$data['code'] = 0;
		$data['msg'] = '批量刷新完成！';
		$this->ajaxReturn($data);
	}

	/** 批量审核  */
	public function batAudit() {
		$result = $this->postModel->select();
		foreach ($result as $v) {
		    $v['status'] = 1;
			$this->postModel->where(array("id" => $v['id']))->save($v);
		}
		
		$data['code'] = 0;
		$data['msg'] = '批量审核完成！';
		$this->ajaxReturn($data);
	}

	/** 审核 */
	public function audit() {
		$id = $_GET['id'];
		$status = $_GET['status'];
		
		$post = $this->postModel->where(array("id" => $id))->find();
		if (!$post) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// 更新status & update
		$post['status'] = $status;
		$post['update_time'] = date("Y-m-d H:i:s");
		
		// 执行更新
		$result = $this->postModel->where(array("id" => $id))->save($post);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '审核失败！';
			$this->ajaxReturn($data);
		}
			
		$data['code'] = 0;
		$data['msg'] = '审核完成！';
		$data['obj'] = $post;
		$this->ajaxReturn($data);
	}
	
	/** 排序 */
	public function sortOrder() {
		$result = parent::_sortOrder($this->postModel);
		if (!$result) {
			$data['code'] = 499;
			$data['msg'] = '编辑排序失败！';
			$this->ajaxReturn($data);
		}

		$data['code'] = 0;
		$data['msg'] = '编辑排序成功！';
		$this->ajaxReturn($data);
	}
	
	/** 删除某信息 */
	public function delete() {
		$id = $_GET['id'];
		
		$post = $this->postModel->where(array("id" => $id))->find();
		if (!$post) {
			$data['code'] = 499;
			$data['msg'] = '未找到目标！';
			$this->ajaxReturn($data);
		}
		
		// 更新status & update
		$post['status'] = -1;

		$result = $this->postModel->where(array("id" => $id))->save($post);
		if (false === $result) {
			$data['code'] = 499;
			$data['msg'] = '删除失败！';
			$this->ajaxReturn($data);
		}
		
		$data['code'] = 0;
		$data['msg'] = '删除成功！';
		$this->ajaxReturn($data);
	}

	/******************************** HTML *********************************/
	// index
    public function index() {
		$this->display(':post');
		return;
	}

	/**----------------------------------- do ----------------------------*/
	public function getIds($data, $pid = 0, $idsList = []) {
	    $idsList[] = (int)$pid;
	    foreach ($data as $k => $v) {
	        if ($v['pid'] == $pid) {
	            $idsList = $this->getIds($data, $v['id'], $idsList);
	        }
	    }
	    return $idsList;
	}
	
}